local shopradius = 75
local sellonlyonce = {"Test2","shield_extender","shield_supercharger","energysucker_1","Looter"}

servertable["shops"] = {}

servertable["shops"]["Olaf's Secondhand Gunshop"] = {}
servertable["shops"]["Olaf's Secondhand Gunshop"]["sells"] = {"pistol","smg1","Test2","shield_extender","shield_supercharger","energysucker_1","Techlevel.2","cooler_upgrade_small","Capital_Ion_Cannon","cargo_upgrade"}
servertable["shops"]["Olaf's Secondhand Gunshop"]["prices"] = {30,120,1000,2000,3000,2500,5000,3000,10000,4000}

servertable["shops"]["Tuning 24/7 Shop"] = {}
servertable["shops"]["Tuning 24/7 Shop"]["sells"] = {"flak_blaster_1000","laser_blaster_1000","RailGun_1000","energy_upgrade_small","energy_storage_100","mininglaser_1","small_supercharger","external_capaciator_small"}
servertable["shops"]["Tuning 24/7 Shop"]["prices"] = {750,300,2000,400,500,1000,1500,1500,1200,3000,500}

servertable["shops"]["Adv. Systems"] = {}
servertable["shops"]["Adv. Systems"]["sells"] = {"autoturret1","Fighter_Ion_Cannon","Looter"}
servertable["shops"]["Adv. Systems"]["prices"] = {1200,3000,500}





local weapons = {"pistol","smg1"}
local sellonlyonce = {"Test2","shield_extender","shield_supercharger"}
local lvl = {}
lvl[1] = {"pistol","Techlevel.2","flak_blaster_1000","laser_blaster_1000","RailGun_1000","energy_upgrade_small","energy_storage_100","mininglaser_1","small_supercharger","external_capaciator_small","Fighter_Ion_Cannon","Looter"}
lvl[2] = {"smg1","Test2","shield_extender","shield_supercharger","energysucker_1","autoturret1","cooler_upgrade_small","Capital_Ion_Cannon","cargo_upgrade"}
 
function Startshop(ply)
	local rp = RecipientFilter()
	rp:AddPlayer( ply )
	pos = ply:GetPos()
	local foundents = ents.FindInSphere(pos,shopradius)
	for k, shop in pairs (foundents) do
		if (shop:IsValid()) then
			Msg(shop:GetName())
			for x, shopname in pairs (servertable["shops"]) do
				if (shop:GetName() == x) then
					umsg.Start("ShopSystem", rp) 
					umsg.String( "Start" )
					umsg.String(shop:GetName())
					umsg.End()  
					for k, laber in pairs (servertable["shops"][shop:GetName()]["sells"]) do
						umsg.Start("ShopSystem", rp) 
						umsg.String("Item")
						umsg.String(Translate(servertable["shops"][shop:GetName()]["sells"][k]))
						umsg.String(servertable["shops"][shop:GetName()]["prices"][k])
						umsg.End()  
					end
					umsg.Start("ShopSystem", rp) 
					umsg.String("Stop")
					umsg.End()  
					return
				end
			end
		end
	end
	ply:SendLua("GAMEMODE:AddNotify(\"" ..  "Shop not found".. "\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
end
concommand.Add("ShopInitialize",Startshop) 


function Transfer2(ply,command,args) 
	local plyuni = ply:UniqueID( )
	local tobuy = TranslateBack(args[1])
	pos = ply:GetPos()
	local foundents = ents.FindInSphere(pos,shopradius)
	for k, shop in pairs (foundents) do
		if (shop:IsValid()) then
			for y, shopname in pairs (servertable["shops"]) do
				if (shop:GetName() == y) then
					for x, item in pairs (servertable["shops"][shop:GetName()]["sells"]) do
						if (item == tobuy) then
							for index=1, tostring(servertable["ply"][plyuni]["techlevel"]), 1 do  
								if table.HasValue(lvl[index],tobuy) then
									if Changemoney(ply,- (servertable["shops"][shop:GetName()]["prices"][x])) then
										for k, weapon in pairs (weapons) do
											if (tobuy == weapon)then 
												ply:StripWeapon ("weapon_" .. tobuy)
												ply:Give("weapon_" .. tobuy)
												ply:SendLua("GAMEMODE:AddNotify(\"" ..  "Recieved weapon" .. tobuy .. " if you buy it again, ammunition will only be restocked".. "\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
												return
											end
										end
										if string.find(tobuy,"Techlevel") then
											Msg("Is a techlevel")
											local techlevel = string.Explode(".",tobuy)
											if (tonumber(techlevel[2]) <= tonumber(servertable["ply"][plyuni]["techlevel"])) then
												Msg("equal Techlvl")
												ply:SendLua("GAMEMODE:AddNotify(\"" ..  "You already have Techlevel " .. techlevel[2] .. " or a higher one" .."\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
												Changemoney(ply,servertable["shops"][shop:GetName()]["price"][x])
												return
											else
												Msg("Higher Techlvl")
												servertable["ply"][plyuni]["techlevel"] = tonumber(techlevel[2])
												ply:SendLua("GAMEMODE:AddNotify(\"" ..  "You advanced to Techlevel " .. techlevel[2] .. "\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
												return
											end
										end
										if table.HasValue(servertable["ply"][plyuni]["count"]["own"]["owns"],tobuy) then
											if table.HasValue(sellonlyonce,tobuy) then
												ply:SendLua("GAMEMODE:AddNotify(\"" ..  "+This can only be bought once " .. Translate(tobuy) .. "\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
											else
												for k, owns in pairs (servertable["ply"][plyuni]["count"]["own"]["owns"]) do
													if (owns == tobuy) then
														servertable["ply"][plyuni]["count"]["own"]["ownamount"][k] = servertable["ply"][plyuni]["count"]["own"]["ownamount"][k] + 1
														ply:SendLua("GAMEMODE:AddNotify(\"" ..  "+1 " .. Translate(tobuy) .. "\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
														return
													end
												end
											end
										else
											local addat = table.getn(servertable["ply"][plyuni]["count"]["own"]["owns"]) +1
											servertable["ply"][plyuni]["count"]["own"]["owns"][addat] = tobuy
											servertable["ply"][plyuni]["count"]["own"]["ownamount"][addat] = 1
											ply:SendLua("GAMEMODE:AddNotify(\"" ..  "+1 " .. Translate(tobuy) .. "\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
											return
										end
									else
										ply:SendLua("GAMEMODE:AddNotify(\"" ..  "You do not have enough money to buy " .. Translate(tobuy) .. "\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
										return
									end
								end
							end
							ply:SendLua("GAMEMODE:AddNotify(\"" ..  "you need a higher Techlvl to buy " .. Translate(tobuy) .. " but you are only lvl ".. servertable["ply"][plyuni]["techlevel"] .."\", " .. "NOTIFY_ERROR" .. ", 5); surface.PlaySound( \"" .. "buttons/button2.wav" .. "\" )")
							return
						end
					end
				end
			end
		end
	end
	ply:PrintMessage(HUD_PRINTCENTER, "No Shop Near" )
end 
concommand.Add("ShopRecive",Transfer2)  